package com.Lgwd.service.chat;

import cn.hutool.core.date.DateUtil;
import com.Lgwd.entity.ChatKey;
import com.Lgwd.entity.Dialogue;
import com.Lgwd.repository.ChatKeyRepository;
import com.Lgwd.repository.DialogueRepository;
import com.Lgwd.utils.ResponseData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @author sunchuanchuan
 * @version V1.0
 * @date 2023/3/22
 * @package com.Lgwd.service.chat
 * @description
 **/
@Component
@Slf4j
public abstract class IChatGptService {


    @Autowired
    private DialogueRepository dialogueRepository;


    @Autowired
    private ChatKeyRepository chatKeyRepository;

    /**
     * 获取gpt key
     * @return
     */
    public abstract String getGptKey();

    /**
     * 获取key到数据库
     * @return
     */
    public abstract ResponseData<?> getKeyToDataBase();

    /**
     * 对话 gpt
     * @param message
     * @return
     */
    public abstract String dialogue(Dialogue message);


    /**
     * 报错对话消息
     * @param dialogue
     * @return
     */
    public Dialogue saveDialogue(Dialogue dialogue){
         return dialogueRepository.save(dialogue);
    }


    /**
     * 保存key
     * @param chatKey
     * @return
     */
    public ChatKey saveChatKey(ChatKey chatKey){
        return chatKeyRepository.save(chatKey);
    }

    /**
     * 查询key
     * @param type
     * @return
     */
    public ChatKey findKey(Integer type){
        String date = DateUtil.formatDate(new Date());
        Integer useDate = Integer.valueOf(date.replaceAll("-",""));
        ChatKey chatKeyByUseDate = chatKeyRepository.findChatKeyByUseDate(type, useDate);
        chatKeyByUseDate.setUseDate(useDate);
        ChatKey save = chatKeyRepository.save(chatKeyByUseDate);
        return save;
    }


    /**
     * 根据对话id查询
     * @param dialogue_id
     * @return
     */
    public List<Dialogue> selectDialogueListByDialogueId(long dialogue_id){
        Dialogue dialogue = new Dialogue();
        dialogue.setDialogue_id(dialogue_id);
        Example<Dialogue> query = Example.of(dialogue);
        Sort sort = Sort.by(Sort.Direction.DESC,"createTime");
       return dialogueRepository.findAll(query,sort);
    }



}
